package com.koi.common.utils.string;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ･ᴗ･
 * @description [异或算法指令]
 */
public class XORCommandUtil {

    /**
     * @description [用于将字节数组转换为十六进制字符串表示]
     * @author ･ᴗ･
     */
    public static String bytesToHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            sb.append(String.format("%02X ", b));
        }
        return sb.toString();
    }


    /**
     * @description [传递单个Byte指令封装]
     * @author ･ᴗ･
     */
    public static String createByteCommand(byte head, byte func, byte data) {
        byte xorResult = head;
        xorResult ^= func;
        xorResult ^= data;
        return String.format("%02X ", head) + String.format("%02X ", func) + String.format("%02X ", data) + String.format("%02X", xorResult);
    }

    /**
     * @description [前两位固定-创建指令]
     * @author ･ᴗ･
     */
    public static String createCommand(byte head, byte func, byte[] data) {
        byte xorResult = head;
        xorResult ^= func;
        for (byte b : data) {
            xorResult ^= b;
        }
        return String.format("%02X ", head) + String.format("%02X ", func) + bytesToHex(data) + String.format("%02X", xorResult);
    }

    /**
     * @description [拼接两个字节数组]
     * @author ･ᴗ･
     */
    public static byte[] concat(byte[] a, byte[] b) {
        byte[] result = new byte[a.length + b.length];
        System.arraycopy(a, 0, result, 0, a.length);
        System.arraycopy(b, 0, result, a.length, b.length);
        return result;
    }


    /**
     * @description [开启指定箱门]
     * @author ･ᴗ･
     */
    public static String openBoxCommand(byte[] bytes) {

        return createCommand((byte) 0x5A, (byte) 0x21, bytes);
    }

    /**
     * @description [byte 转为二进制]
     * @author ･ᴗ･
     */
    public static List<Integer> byteToBinary(byte value) {
        String binaryStr = Integer.toBinaryString(value);
        return reverseString(binaryStr);
    }

    /**
     * @description [翻转字符串 并 获取哪个位置是1]
     * @author ･ᴗ･
     */
    public static List<Integer> reverseString(String value) {
        String reverseBuilder = new StringBuilder(value).reverse().toString();
        List<Integer> IntergerList = new ArrayList<>();
        for (int i = 0; i < reverseBuilder.length(); i++) {
            if (String.valueOf(reverseBuilder.charAt(i)).equals("1")) {
                IntergerList.add(i + 1);
            }
        }
        return IntergerList;
    }


    public static void main(String[] args) {
        // String转byte
        // String byteBoxId = "0x" + boxId;
        // int intValue = Integer.decode(byteBoxId); // 解析十六进制字符串为整数
        // byte byteValue = (byte) intValue; // 将整数强制转换为字节

        // 设置起始结束箱号
        byte[] startBoxCh = {0x00, 0x01};
        byte[] endBoxCh = {0x00, 0x01};

        // 设置箱号
        String setBoxCommand = createCommand((byte) 0x5A, (byte) 0x23, concat(startBoxCh, endBoxCh));

        // 开启指定箱门
        String openBoxCommand = createCommand((byte) 0x5A, (byte) 0x21, new byte[]{0x01, 0x01});

        // 开启照明
        String openLightCommand = createCommand((byte) 0x5A, (byte) 0x28, new byte[]{0x00, 0x01});

        // 关闭照明
        String closeLightCommand = createCommand((byte) 0x5A, (byte) 0x29, new byte[]{0x00, 0x01});

        // 查询状态
        String queryStatusCommand = createCommand((byte) 0x5A, (byte) 0x22, new byte[]{0x00, 0x01});

        // 新查询状态
        String newQueryStatusCommand = createByteCommand((byte) 0x5A, (byte) 0x22, (byte) 0x01);

        // 打印指令
        System.out.println("设置箱号：" + setBoxCommand);

        System.out.println("开启指定箱门：" + openBoxCommand);
        System.out.println("开启指定箱门：" + XORCommandUtil.openBoxCommand(new byte[]{0x01, 0x01}));
        System.out.println("开启照明：" + openLightCommand);
        System.out.println("关闭照明：" + closeLightCommand);
        System.out.println("查询状态：" + queryStatusCommand);
        System.out.println("新查询状态：" + newQueryStatusCommand);
        System.out.println("二进制翻转，数值为1位置：" + reverseString("00100101"));

        // 案例
        String boxId = "01";
        String byteBoxId = "0x" + boxId;
        int intValue = Integer.decode(byteBoxId); // 解析十六进制字符串为整数
        byte byteValue = (byte) intValue; // 将整数强制转换为字节
        System.out.println("开启指定箱门[案例]：" + XORCommandUtil.openBoxCommand(new byte[]{0x00, byteValue}));
    }


}
